﻿using AutoMapper;
using DDD.Application;
using Microsoft.Extensions.Logging;
using OrderModule.Domain.OrderAggregate;
using OrderModule.Infrastructure.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderModule.Application.Commands
{
    public class OrderStatusPendingPayCommand : ICommand<Domain.OrderAggregate.Order>
    {
        public OrderStatusPendingPayCommand(int orderId) 
        {
            OrderId = orderId;
        }

        public int OrderId { get; }
    }

    public class OrderStatusPendingPayCommandHandler(
       IMapper mapper,
       OrderRepository repository,
       ILogger<OrderStatusPendingPayCommandHandler> logger
       )
       : ICommandHandler<OrderStatusPendingPayCommand, Domain.OrderAggregate.Order>
    {
        public async Task<Order> Handle(OrderStatusPendingPayCommand request, CancellationToken cancellationToken)
        {
            var order = await repository.GetAsync(request.OrderId) ?? throw new Exception($"找不到ID为{request.OrderId}的订单");
            order.ChangeStatus("待支付");
            return order;
        }
    }
}
